package com.pl.museumguide.activities;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Scanner;
import com.pl.museumguide.R;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Environment;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.RelativeLayout;

public class navigation extends Activity {

	protected Button upButton;
	protected Button downButton;
	protected Button rightButton;
	protected Button leftButton;

	private RelativeLayout layout;			
	String currView = "Room1";				//Zmienna odpowiedzialna za aktualnie ogladany ekran
	File image, up, down, right, left, tmp;	//Zmienne odpowiedzialne za odwzorowanie plikow znajdujacych sie w folderach pokoi
	Scanner scanner;
	View v;
	Drawable mDrawable = null;				//Zmienna do przechowywania plikow .jpg przekonwertowanych do Drawable
	CharSequence[] items;

	//Funkcja zapisujaca stringa przechowywanego w pliku do zmiennej currView
	void scan(File a) {
		try {
			scanner = new Scanner(a);
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		currView = scanner.next();
		
	}
	//Funkcja do odswiezania ekranu. Ustawia widocznosc przyciskow jezeli plik istnieje, ustawa nowy background i odswieza widok
	void refreshView() {
		if (image.exists()) {
			mDrawable = Drawable.createFromPath(image.getAbsolutePath());
		}
		if (up.exists()) {
			upButton.setVisibility(View.VISIBLE);
		} else upButton.setVisibility(View.INVISIBLE);
			
		if (down.exists()) {
			downButton.setVisibility(View.VISIBLE);
		} else downButton.setVisibility(View.INVISIBLE);
		if (right.exists()) {
			rightButton.setVisibility(View.VISIBLE);
		} else rightButton.setVisibility(View.INVISIBLE);
		if (left.exists()) {
			leftButton.setVisibility(View.VISIBLE);
		} else leftButton.setVisibility(View.INVISIBLE);
		
		layout = (RelativeLayout) findViewById(R.id.navlayout);
		layout.setBackgroundDrawable(mDrawable);
		this.setTitle(currView);
		layout.requestLayout();
	}
	//Funckja ladujaca pliki z karty SD do zmiennej typu File
	void loadFiles() {
		image = new File(Environment.getExternalStorageDirectory()
				+ File.separator + "MuseumGuide" + File.separator
				+ "Zwiedzanie" + File.separator + currView + File.separator
				+ "background.jpg");
		up = new File(Environment.getExternalStorageDirectory()
				+ File.separator + "MuseumGuide" + File.separator
				+ "Zwiedzanie" + File.separator + currView + File.separator
				+ "up.txt");
		down = new File(Environment.getExternalStorageDirectory()
				+ File.separator + "MuseumGuide" + File.separator
				+ "Zwiedzanie" + File.separator + currView + File.separator
				+ "down.txt");
		right = new File(Environment.getExternalStorageDirectory()
				+ File.separator + "MuseumGuide" + File.separator
				+ "Zwiedzanie" + File.separator + currView + File.separator
				+ "right.txt");
		left = new File(Environment.getExternalStorageDirectory()
				+ File.separator + "MuseumGuide" + File.separator
				+ "Zwiedzanie" + File.separator + currView + File.separator
				+ "left.txt");
	}
	
	@Override
	public boolean onPrepareOptionsMenu(final Menu menu) {
		tmp = new File(Environment.getExternalStorageDirectory()
				+ File.separator + "MuseumGuide" + File.separator
				+ "Zwiedzanie" + File.separator + currView + File.separator
				+ "zabytki.txt");
		if(tmp.exists()){
		try {
			scanner = new Scanner(tmp);
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		ArrayList<String> list1 = new ArrayList<String>();
		while (scanner.hasNext()){
			    list1.add(scanner.next());
		}
		items = new CharSequence[list1.size()];
		for(int x=0; x<list1.size(); x++){
			items[x] = list1.get(x).toString();
		}
		
		AlertDialog.Builder builder = new AlertDialog.Builder(this);
		builder.setTitle("Wybierz zabytek");
		builder.setItems(items, new DialogInterface.OnClickListener() {
		    public void onClick(DialogInterface dialog, int item) {
		        
		        Intent i = new Intent(getApplicationContext(), Wyswietl.class);
		        String selection = items[item].toString();
				i.putExtra("nazwa",selection);
				startActivity(i);
		    }
		});
		AlertDialog alert = builder.create();
		alert.show();
		}else{
			AlertDialog.Builder builder = new AlertDialog.Builder(this);
			builder.setTitle("Brak zabytków w danej lokalizacji");
			AlertDialog alert = builder.create();
			alert.show();
		}
		
	    return super.onPrepareOptionsMenu(menu);
	}
	
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.navigation_layout);

		upButton = (Button) findViewById(R.id.ButtonUp);
		downButton = (Button) findViewById(R.id.ButtonDown);
		rightButton = (Button) findViewById(R.id.ButtonRight);
		leftButton = (Button) findViewById(R.id.ButtonLeft);

		loadFiles();
		refreshView();
		
		upButton.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				scan(up);
				loadFiles();
				refreshView();
			}
		});
		downButton.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				scan(down);
				loadFiles();
				refreshView();
			}
		});
		rightButton.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				scan(right);
				loadFiles();
				refreshView();
			}
		});
		leftButton.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				scan(left);
				loadFiles();
				refreshView();
			}
		});

	}

}
